{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# A Cantera Simulation Using RMG-Py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from rmgpy.chemkin import *\n",
    "from rmgpy.tools.canteraModel import *\n",
    "from rmgpy.species import Species\n",
    "from IPython.display import display, Image"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Load the species and reaction from the RMG-generated chemkin file `chem_annotated.inp` and `species_dictionary.txt` file found in your `chemkin` folder after running a job."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "speciesList, reactionList = loadChemkinFile('data/minimal_model/chem_annotated.inp',\n",
    "                                            'data/minimal_model/species_dictionary.txt',\n",
    "                                           'data/minimal_model/tran.dat')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Set a few conditions for how to react the system"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Find the species: ethane\n",
    "user_ethane=Species().fromSMILES('CC')\n",
    "speciesDict = getRMGSpeciesFromUserSpecies([user_ethane], speciesList)\n",
    "ethane = speciesDict[user_ethane]\n",
    "\n",
    "reactorTypeList = ['IdealGasReactor']\n",
    "molFracList=[{ethane: 1}]\n",
    "Tlist = ([1300,1500,2000],'K')\n",
    "Plist = ([1],'atm')\n",
    "reactionTimeList = ([0.5], 'ms')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create cantera object, loading in the species and reactions\n",
    "job = Cantera(speciesList=speciesList, reactionList=reactionList, outputDirectory='temp')\n",
    "# The cantera file must be created from an associated chemkin file\n",
    "\n",
    "# We can either load the Model from the initialized set of rmg species and reactions\n",
    "job.loadModel()\n",
    "\n",
    "# Or load it from a chemkin file by uncommenting the following line:\n",
    "#job.loadChemkinModel('data/minimal_model/chem_annotated.inp',transportFile='data/minimal_model/tran.dat')\n",
    "\n",
    "# Generate the conditions based on the settings we declared earlier\n",
    "job.generateConditions(reactorTypeList, reactionTimeList, molFracList, Tlist, Plist)\n",
    "# Simulate and plot\n",
    "alldata = job.simulate()\n",
    "job.plot(alldata)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We can view the cantera model Solution's species and reactions\n",
    "ctSpecies = job.model.species()\n",
    "ctReactions = job.model.reactions()\n",
    "\n",
    "# We can view a cantera species or reaction object from this\n",
    "ct_ethane = ctSpecies[4]\n",
    "ct_rxn = ctReactions[0]\n",
    "\n",
    "print ct_ethane\n",
    "print ct_rxn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# We can also do things like modifying the cantera species thermo and reaction kinetics through modifying the\n",
    "# RMG objects first, then using the `modifyReactionKinetics` or `modifySpeciesThermo` functions\n",
    "\n",
    "# Alter the RMG objects in place, lets pick ethane and the first reaction\n",
    "rmg_ethane = speciesDict[user_ethane]\n",
    "rmg_ethane.thermo.changeBaseEnthalpy(2*4184)  # Change base enthalpy by 2 kcal/mol\n",
    "\n",
    "rmg_rxn = reactionList[0]\n",
    "rmg_rxn.kinetics.changeRate(4)  # Change A factor by multiplying by a factor of 4\n",
    "\n",
    "# Take a look at the state of the cantera model before and after\n",
    "print 'Cantera Model: Before'\n",
    "ctSpecies = job.model.species()\n",
    "ctReactions = job.model.reactions()\n",
    "print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)\n",
    "print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)\n",
    "\n",
    "# Now use the altered RMG objects to modify the kinetics and thermo\n",
    "job.modifyReactionKinetics(0, rmg_rxn)\n",
    "job.modifySpeciesThermo(4, rmg_ethane, useChemkinIdentifier = True)\n",
    "\n",
    "# If we modify thermo, the cantera model must be refreshed.  If only kinetics are modified, this does not need to be done.\n",
    "job.refreshModel()\n",
    "\n",
    "print ''\n",
    "print 'Cantera Model: After'\n",
    "ctSpecies = job.model.species()\n",
    "ctReactions = job.model.reactions()\n",
    "print 'Ethane Thermo = {} kcal/mol'.format(ctSpecies[4].thermo.h(300)/1000/4184)\n",
    "print 'Reaction 1 Kinetics = {}'.format(ctReactions[0].rate)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show the plots in the ipython notebook\n",
    "for i, condition in enumerate(job.conditions):\n",
    "    print 'Condition {0}'.format(i+1)\n",
    "    display(Image(filename=\"temp/{0}_mole_fractions.png\".format(i+1)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2.0
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}